using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using System;

namespace MIMPWinForms
{
    /// <summary>
    /// Helper for reading input from keyboard and mouse. This class tracks the 
    /// current and previous state of both input devices, and provides methods
    /// to determine whether a key or mouse button is down, as well as whether it
    /// was newly pressed or newly released during this update.
    /// </summary>
    public class InputState
    {
        public KeyboardState CurrentKeyboardState;

        public MouseState CurrentMouseState;

        public KeyboardState PreviousKeyboardState;

        public MouseState PreviousMouseState;

        /// <summary>
        /// Constructs a new input state.
        /// </summary>
        public InputState() {
            CurrentKeyboardState = new KeyboardState();
            CurrentMouseState = new MouseState();

            PreviousKeyboardState = new KeyboardState();
            PreviousMouseState = new MouseState();
        }

        /// <summary>
        /// Reads the latest state of the keyboard and gamepad.
        /// </summary>
        public void Update() {
            PreviousMouseState = CurrentMouseState;
            CurrentMouseState = Mouse.GetState();

            if (CurrentMouseState.LeftButton == ButtonState.Pressed) {

            }

            PreviousKeyboardState = CurrentKeyboardState;
            CurrentKeyboardState = Keyboard.GetState();
        }


        /// <summary>
        /// True if the specified key was newly pressed during this update, false otherwise.
        /// </summary>
        public bool IsNewKeyPress(Keys key) {
            return CurrentKeyboardState.IsKeyDown(key) && PreviousKeyboardState.IsKeyUp(key);
        }
        /// <summary>
        /// True if the specified key was newly released during this update, false otherwise.
        /// </summary>
        public bool IsNewKeyRelease(Keys key) {
            return CurrentKeyboardState.IsKeyUp(key) && PreviousKeyboardState.IsKeyDown(key);
        }
        /// <summary>
        /// True if the specified key is down druing this update, false otherwise.
        /// </summary>
        public bool IsKeyDown(Keys key) {
            return CurrentKeyboardState.IsKeyDown(key);
        }
        /// <summary>
        /// True if the specified key is up druing this update, false otherwise.
        /// </summary>
        public bool IsKeyUp(Keys key) {
            return CurrentKeyboardState.IsKeyUp(key);
        }
        /// <summary>
        /// True if the left mouse button was newly released during this update, false otherwise.
        /// </summary>
        /// <returns></returns>
        public bool IsNewLeftMouseClick() {
            return (CurrentMouseState.LeftButton == ButtonState.Released &&
                    PreviousMouseState.LeftButton == ButtonState.Pressed);
        }
        /// <summary>
        /// True if the right mouse button was newly released during this update, false otherwise.
        /// </summary>
        /// <returns></returns>
        public bool IsNewRightMouseClick() {
            return (CurrentMouseState.RightButton == ButtonState.Released &&
                    PreviousMouseState.RightButton == ButtonState.Pressed);
        }
        /// <summary>
        /// True if the left mosue button is down druing this update, false otherwise. 
        /// </summary>
        public bool IsLeftMouseDown {
            get { return CurrentMouseState.LeftButton == ButtonState.Pressed; }
        }
        /// <summary>
        /// True if the right mosue button is down druing this update, false otherwise. 
        /// </summary>
        public bool IsRightMouseDown {
            get { return CurrentMouseState.RightButton == ButtonState.Pressed; }
        }
        /// <summary>
        /// True if the left mosue button is up druing this update, false otherwise. 
        /// </summary>
        public bool IsLeftMouseUp {
            get { return CurrentMouseState.LeftButton == ButtonState.Released; }
        }
        /// <summary>
        /// True if the right mosue button is up druing this update, false otherwise. 
        /// </summary>
        public bool IsRightMouseUp {
            get { return CurrentMouseState.RightButton == ButtonState.Released; }
        }

        public ConsoleModifiers GetModifiers() {
            ConsoleModifiers mods = 0;
            if (CurrentKeyboardState.IsKeyDown(Keys.LeftShift) || CurrentKeyboardState.IsKeyDown(Keys.RightShift)) {
                mods |= ConsoleModifiers.Shift;
            }
            if (CurrentKeyboardState.IsKeyDown(Keys.LeftControl) || CurrentKeyboardState.IsKeyDown(Keys.RightControl)) {
                mods |= ConsoleModifiers.Control;
            }
            if (CurrentKeyboardState.IsKeyDown(Keys.LeftAlt) || CurrentKeyboardState.IsKeyDown(Keys.RightAlt)) {
                mods |= ConsoleModifiers.Alt;
            }
            return mods;
        }
    }
}
